package Servlet;

import Dao.UserDAOProxy;
import Entity.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        //创建用来显示登录结果信息的列表
        List<String> info=new ArrayList<String>();
        if(name==null||"".equals(name)){ //用户名输入格式问题
            info.add("用户名不能为空");
            System.out.println("用户名不能为空");
        }

        if(password==null||"".equals(password)){//密码输入格式问题
            info.add("密码不能为空");
            System.out.println("密码不能为空");
        }
        // 如果用户名、密码不为空
        if(info.size()==0){
            //先创建一个实体类对象
            User user=new User();
            //使用用户输入的用户名、密码，作为其字段
            user.setName(name);
            user.setPassword(password);
            //数据代理
            UserDAOProxy userDAOProxy=new UserDAOProxy();
            try {
                //查找当前用户在数据库中是否存在
                if(userDAOProxy.findLogin(user)){
                    info.add("用户登录成功，欢迎"+user.getName()+"光临！");
                }else {
                    info.add("用户登录失败，错误的用户名和密码");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        request.setAttribute("info", info);//保存错误信息
        request.getRequestDispatcher("Login.jsp").forward(request,response);//跳转

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
